package com.hosp.service;

import java.util.List;
import java.util.Objects;

import cn.hutool.core.date.DateUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hosp.bean.Chargeproject;
import com.hosp.bean.ChargeprojectExample;
import com.hosp.mapper.ChargeprojectMapper;
import com.hosp.vo.PageRequestVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

/**
 * @author ztranscript 2021/10/16 11:11
 **/
@Slf4j
@Service
public class ChargeProjectervice {
    @Autowired
    private ChargeprojectMapper chargeprojectMapper;

    /**
     * 查询列表
     *
     * @param chargeproject 查询条件
     * @param pageRequest   分页条件
     * @return 响应
     */
    public PageInfo<Chargeproject> list(Chargeproject chargeproject, PageRequestVO pageRequest) {
        ChargeprojectExample example = new ChargeprojectExample();
        if (Objects.nonNull(chargeproject) && !StringUtils.isEmpty(chargeproject.getChapName())) {
            String chapName = chargeproject.getChapName();
            example.createCriteria().andChapNameLike("%" + chapName + "%");
        }
        // 分页查询
        PageHelper.startPage(pageRequest.getPageNum(), pageRequest.getPageSize());
        List<Chargeproject> chargeprojects = chargeprojectMapper.selectByExample(example);
        return new PageInfo<>(chargeprojects);
    }

    /**
     * 保存
     *
     * @param chargeproject 保存对象
     * @return 响应
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean save(Chargeproject chargeproject) {
        if (Objects.isNull(chargeproject.getChapId())) {
            chargeproject.setChapTime(DateUtil.date());
            chargeprojectMapper.insertSelective(chargeproject);
        } else {
            chargeprojectMapper.updateByPrimaryKeySelective(chargeproject);
        }
        return true;
    }

    /**
     * 主键查询
     *
     * @param chapId 查询条件
     * @return 响应
     */
    public Chargeproject selectById(Integer chapId) {
        return chargeprojectMapper.selectByPrimaryKey(chapId);
    }

    /**
     * 删除
     *
     * @param chapIds 待删除 IDs
     * @return 响应
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(List<Integer> chapIds) {
        for (Integer chapId : chapIds) {
            chargeprojectMapper.deleteByPrimaryKey(chapId);
        }
        return true;
    }

    /**
     * 查询集合
     *
     * @param chargeproject 查询条件
     * @return 响应
     */
    public List<Chargeproject> search(Chargeproject chargeproject) {
        ChargeprojectExample example = new ChargeprojectExample();
        ChargeprojectExample.Criteria criteria = example.createCriteria();
        String chapName = chargeproject.getChapName();
        criteria.andChapNameLike("%" + chapName + "%");
        return chargeprojectMapper.selectByExample(example);
    }

    public boolean check(String chapName) {
        ChargeprojectExample example = new ChargeprojectExample();
        ChargeprojectExample.Criteria criteria = example.createCriteria();
        criteria.andChapNameEqualTo(chapName);
        return chargeprojectMapper.selectByExample(example).size() == 0;
    }
}
